package com.bjpowernode.rbac.web.controller;

import com.bjpowernode.rbac.anno.RequestPermission;
import com.bjpowernode.rbac.entity.Department;
import com.bjpowernode.rbac.entity.Employee;
import com.bjpowernode.rbac.entity.Role;
import com.bjpowernode.rbac.query.EmployeeQuery;
import com.bjpowernode.rbac.query.QueryObject;
import com.bjpowernode.rbac.service.DepartmentService;
import com.bjpowernode.rbac.service.EmployeeService;
import com.bjpowernode.rbac.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author jy
 * @version 1.0
 * @date 2022/2/7 19:13
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    @RequestPermission({"员工列表权限","employee:list"})
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery employeeQuery){

        employeeQuery.setPageSize(0);
        PageInfo<Department> departmentPageInfo = departmentService.selectDepartmentByCondition(employeeQuery);
        model.addAttribute("departments",departmentPageInfo.getList());

        employeeQuery.setPageSize(2);
        //调用业务层获取数据
        PageInfo<Employee> pageInfo = employeeService.selectEmployeeByCondition(employeeQuery);
        //传到页面
        model.addAttribute("pageInfo",pageInfo);
        //跳转到页面
        return "employee/list";
    }

    @RequestMapping("/input")
    @RequestPermission({"员工新增和编辑权限","employee:input"})
    public String input(Long id,Model model){
        /**
         * id不为空即是编辑，则需要查询数据库回显表单
         */
        if (id != null){
            Employee employee = employeeService.selectEmpById(id);
            model.addAttribute("employee",employee);
        }
        //否则为添加,添加页面里面又需要把部门全部查出来
        EmployeeQuery employeeQuery = new EmployeeQuery();
        employeeQuery.setPageSize(0);
        PageInfo<Department> departmentPageInfo = departmentService.selectDepartmentByCondition(employeeQuery);
        model.addAttribute("departments",departmentPageInfo.getList());

        List<Role> roles = roleService.selectAllRoles();
        model.addAttribute("roles",roles);
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequestPermission({"员工新增和编辑权限","employee:saveOrUpdate"})
    public String saveOrUpdate(Employee employee,Long[] roleIds){
        employeeService.saveOrUpdate(employee,roleIds);
        return "redirect:/employee/list";
    }

    @RequestMapping("/delete")
    @RequestPermission({"员工删除","employee:delete"})
    public String delete(Long id){
        employeeService.delete(id);
        return "redirect:/employee/list";
    }
}
